package com.example.idachuappone.order.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.order.entity.ImageBucket;
import com.example.idachuappone.order.imageutil.BitmapCache;
import com.example.idachuappone.order.imageutil.BitmapCache.ImageCallback;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class ImageBucketAdapter extends BaseAdapter {
	BitmapCache cache;
	ImageCallback callback;
	private Context context;
	private List<ImageBucket> dataList;

	private class ViewHolder {
		private TextView count;
		private ImageView image;
		private TextView name;
		private ImageView selected;

		private ViewHolder() {
		}
	}

	public ImageBucketAdapter(Context context) {
		this.callback = new ImageCallback() {
			@Override
			public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
				if (imageView != null && bitmap != null) {
					String url = (String) params[0];
					if (url != null && url.equals(imageView.getTag())) {
						imageView.setImageBitmap(bitmap);
					}
				}
			}
		};
		this.context = context;
		this.cache = new BitmapCache();
	}

	public void setDataList(List<ImageBucket> dataList) {
		this.dataList = dataList;
	}

	@Override
	public int getCount() {
		return this.dataList == null ? 0 : this.dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.dataList == null ? null : (ImageBucket) this.dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_image_bucket, null);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView.findViewById(R.id.image);
			holder.selected = (ImageView) convertView.findViewById(R.id.isselected);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.count = (TextView) convertView.findViewById(R.id.count);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ImageBucket imageBucket = this.dataList.get(position);
		holder.name.setText(imageBucket.bucketName);
		holder.count.setText(new StringBuilder(String.valueOf(imageBucket.count)).toString());
		if (imageBucket.imageList == null || imageBucket.imageList.size() <= 0) {
			holder.image.setImageBitmap(null);
		} else {
			String thumbPath = imageBucket.imageList.get(0).thumbnailPath;
			String sourcePath = imageBucket.imageList.get(0).imagePath;
			holder.image.setTag(sourcePath);
			this.cache.displayBmp(holder.image, thumbPath, sourcePath, this.callback);
		}
		holder.selected.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		return convertView;
	}
}
